﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;

public partial class AdminX_News_NewsCatMain : System.Web.UI.UserControl
{  
    #region Define Var
    int nPageCount = 0;
    int nPageSize = 0;
    private int nDefLang = Support.GetDefaultLanguage();
    protected const string VERTICAL_COLUMN_NAME = "VerticalSort";
    protected const string HOLIZOTAL_COLUMN_NAME = "HolizotalSort";
    private DataTable mCateData = new DataTable();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        if (!IsPostBack)
        {
            SetLableLang();
            BindDataToLanguageDropDown();
            //ShowMess();
            BindData(1);
        }
    }

    private void SetLableLang()
    {
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefLang - 1];
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefLang - 1];
        
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
   
    
    private void BindData(int CurrentPageIndex)
    {
        int nPageSize = Config.PAGESIZE;
        int nPageCount = Config.PAGECOUNT;

        DataTable dtbPage = new DataTable();
        NewsCategory objPage = new NewsCategory();

        dtbPage = objPage.Search();
        FlowCategory flow = new FlowCategory(dtbPage, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);

        DataTable objTable = new DataTable();
        NewsCategory obj = new NewsCategory();
        objTable = obj.Search();

        FlowCategory objflow = new FlowCategory(objTable, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);
        objflow.IDColumnName = "NewsCategoryID";
        objflow.ParentIDColumName = "[Parent]";

        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLang)
        {
            case 1:
                flow.SortColumnName = "Description1";
                objflow.SortColumnName = "Description1";
                break;
            case 2:
                flow.SortColumnName = "Description2";
                objflow.SortColumnName = "Description2";
                break;
            case 3:
                flow.SortColumnName = "Description3";
                objflow.SortColumnName = "Description3";
                break;
        }

        objflow.ParentSortDirection = "ASC";

        if (objflow.Execute(0))
        {
            int iCurrIndex = 0;
            if (CurrentPageIndex > 0) iCurrIndex = CurrentPageIndex - 1;
            mCateData = objflow.DestinationData();
            FlowPager pager = new FlowPager(mCateData, nPageSize, "[Parent]");

            DataView vwPage = pager.GeneralPageByIndex(iCurrIndex, VERTICAL_COLUMN_NAME);

            RpNewsCategory.DataSource = vwPage;


            nPageCount = mCateData.Rows.Count;
            nPageSize = pager.TotalRecord;
        }

        mCateData = objflow.DestinationData();

        RpNewsCategory.DataBind();
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + " " + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
       
    }
    protected string CreateMarginLeft(object iHolizoltal, int pID)
    {
        int iParentID = 1;
        int marginLeft = 0;
        if (!Convert.IsDBNull(iHolizoltal))
        {
            int iHolzl = Convert.ToInt16(iHolizoltal);
            marginLeft = iHolzl * 80;
        }
        if (marginLeft == 0)
        {
            iParentID = pID;
        }
        string fontBold = (marginLeft == 0) ? "font-weight:bold;" : String.Empty;
        return "margin-left:" + marginLeft + "px;" + fontBold;
    }
    protected void RpNewsCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        NewsCategory objNewsCategory = new NewsCategory();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objNewsCategory.DeleteById(nID);
                //if (nDelete > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindData(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_NEWSCAT_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
        }
    }
    protected void RpNewsCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblID = (Label)e.Item.FindControl("lblID");
            Label lblCategoryName = (Label)e.Item.FindControl("lblCategoryName");
            int intParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));

            lblID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsCategoryID"));
            switch (nCurrentLang)
            {
                case 1:
                    lblCategoryName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description1"));
                    break;
                case 2:
                    lblCategoryName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description2"));
                    break;
                case 3:
                    lblCategoryName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description3"));
                    break;
            }

            if (intParent > 0) // is Child
                lblCategoryName.CssClass = "MarginLeft";
        }

        if (e.Item.ItemType != ListItemType.Footer && e.Item.ItemType != ListItemType.Header)
        {
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");

            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsCategoryID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaultLanguage]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsCategoryID"));
        }
    }

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAddNew = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_NEWSCAT_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAddNew);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData(1);
    }
 
}